package com.tiantian.quanminxiu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.heepay.plugin.activity.Constant;
import com.heepay.plugin.api.HeepayPlugin;
import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.adapter.RechargeAdapter;
import com.tiantian.quanminxiu.event.LiveRoomEvent;
import com.tiantian.quanminxiu.presenter.RechargePresent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.ActivityBase;
import com.tiantian.quanminxiu.utils.EventBusUtils;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.RechargeView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Activity_Recharge extends ActivityBase implements RechargeView{
	// 余额
	private TextView tv_recharge_remains;
	private RelativeLayout rl_recharge_chosetype;
	private TextView tv_recharge_chosetype_text;
	private ImageView iv_recharge_chosetype_alipay;
	private ImageView iv_recharge_chosetype_wechatpay;
	private LinearLayout ll_recharge_chosekuang;
	private String bill_id;
	TextView tv_recharge_sure;
	ImageView iv_recharge_userimage;

	private RechargePresent present;

	private RecyclerView mRecyclerView;
	private RechargeAdapter adapter;
	private String strList = "[{\"PersonCoin\":10,\"GameCoin\":\"100金币\",\"select\":-1},{\"PersonCoin\":20,\"GameCoin\":\"200金币\",\"select\":-1},{\"PersonCoin\":50,\"GameCoin\":\"500金币\",\"select\":-1},{\"PersonCoin\":100,\"GameCoin\":\"1000金币\",\"select\":-1},{\"PersonCoin\":200,\"GameCoin\":\"2000金币\",\"select\":-1},{\"PersonCoin\":500,\"GameCoin\":\"5000金币\",\"select\":-1},{\"PersonCoin\":1000,\"GameCoin\":\"10000金币\",\"select\":-1},{\"PersonCoin\":2000,\"GameCoin\":\"20000金币\",\"select\":-1},{\"PersonCoin\":3000,\"GameCoin\":\"30000金币\",\"select\":-1}]";
	private JSONArray list;

	private int recharCount;
	private boolean isGiftList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_recharge);

		isGiftList = getIntent().getBooleanExtra("isGiftList",false);

		Init();

		InitView();

		present = new RechargePresent();
		present.attachView(this);

		UserData.Reload.Activity_Recharge = true;
	}
	@Override
	public void InitView() {
		mRecyclerView = (RecyclerView) findViewById(R.id.mRecyclerView);
		GridLayoutManager manager = new GridLayoutManager(thisActivity,2);
		mRecyclerView.setLayoutManager(manager);

		iv_recharge_userimage = (ImageView) findViewById(R.id.iv_recharge_userimage);
		tv_recharge_sure = (TextView) findViewById(R.id.tv_recharge_sure);
		tv_recharge_remains = (TextView) findViewById(R.id.tv_recharge_remains);
		rl_recharge_chosetype = (RelativeLayout) findViewById(R.id.rl_recharge_chosetype);
		tv_recharge_chosetype_text = (TextView) findViewById(R.id.tv_recharge_chosetype_text);
		iv_recharge_chosetype_alipay = (ImageView) findViewById(R.id.iv_recharge_chosetype_alipay);
		iv_recharge_chosetype_wechatpay = (ImageView) findViewById(R.id.iv_recharge_chosetype_wechatpay);
		ll_recharge_chosekuang = (LinearLayout) findViewById(R.id.ll_recharge_chosekuang);

		iv_recharge_chosetype_alipay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//pay_type 22是支付宝 30 是微信
				if(recharCount<0){
					CommonUtils.showToask(thisContext,"请选择充值金额..");
					return;
				}
				present.rechargeSubToXiu(22,recharCount , "xxxx");
			}
		});

		iv_recharge_chosetype_wechatpay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//pay_type 22是支付宝 30 是微信
				CommonUtils.showToask(thisContext,getResources().getString(R.string.not_yet_completed));
				/*if(recharCount<0){
					CommonUtils.showToask(thisContext,"请选择充值金额..");
					return;
				}
				present.rechargeSubToXiu(30,recharCount, "xxxx");*/
			}
		});
		rl_recharge_chosetype.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				rl_recharge_chosetype.setVisibility(View.GONE);
				ll_recharge_chosekuang.setVisibility(View.GONE);
			}
		});
		tv_recharge_sure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				choseType();
			}
		});
	}

	@Override
	public void PageInit() {
		if(!UserData.Reload.Activity_Recharge){
			return;
		}
		UserData.Reload.Activity_Recharge = false;
		try {
			list = new JSONArray(strList);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		mRecyclerView.setAdapter(adapter = new RechargeAdapter(thisActivity,list));
		adapter.setListener(new RechargeAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(JSONObject value) {
				recharCount = value.optInt("PersonCoin");
			}
		});
		tv_recharge_remains.setText(UserData.Base.Coin + "");
		APPUtils.displayImage(APPUtils.createUserAvatar(UserData.Base.User_ID), iv_recharge_userimage);
	}

	public void choseType() {
		tv_recharge_chosetype_text.setText("充值金额:"+recharCount+"0金币（￥"+recharCount+"）");
		rl_recharge_chosetype.setVisibility(View.VISIBLE);
		ll_recharge_chosekuang.setVisibility(View.VISIBLE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		rl_recharge_chosetype.setVisibility(View.GONE);
		ll_recharge_chosekuang.setVisibility(View.GONE);
		if (resultCode == Constant.RESULTCODE) {
			String respCode = data.getExtras().getString("respCode");
			String respMessage = data.getExtras().getString("respMessage","");
			if (!TextUtils.isEmpty(respCode)) {
				if ("01".equals(respCode)) {
					CommonUtils.showToask(thisContext, "充值成功");
					UserData.Reload.Activity_Recharge = true;
					UserData.Reload.Fragment_Mine = true;
					UserData.Base.Coin = UserData.Base.Coin+recharCount*10;
					if (isGiftList) {
						EventBusUtils.getInstance().post(new LiveRoomEvent("","RechargeSuccess"));
					}
					PageInit();
				}
				if ("00".equals(respCode)) {
					CommonUtils.showToask(thisContext, "订单处理中");
				}
				if ("-1".equals(respCode)) {
					CommonUtils.showToask(thisContext, "充值失败:"+respMessage);
				}
			}
		}
	}
	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		PageInit();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		present.detachView();
	}

	//region 接收汇付宝服务器 rechargeSubToHeePay
	@Override
	public void rechargeSubToHeePay(String param) {
		CommonUtils.showToask(thisContext, "正在启动支付控件...");
		Log.i("jj","--dd:"+param);
		HeepayPlugin.pay(this, param);
	}
	//endregion
}
